<!-- 主页 -->
<template>
	<view class="outer">
		<shop v-if="tarIndex === 0"></shop>
		<order v-if="tarIndex === 1"></order>
		<shopCart v-if="tarIndex === 2"></shopCart>
		<myAccount v-if="tarIndex === 3"></myAccount>
		<tabBar :name="tarIndex" @changeTab="changeTab()"></tabBar>
		<template>
			<u-modal :show="showModel" :title="title"  @confirm='closeModel'>
				<view class="slot-content">
					<rich-text :nodes="content"></rich-text>
				</view>
			</u-modal>
		</template>
	</view>
</template>

<script>
	import tabBar from "../../components/tarbar.vue"
	import shop from "../shop/shop.vue"
	import order from "../order/order.vue"
	import myAccount from "../my-account/my-account.vue"
	import shopCart from "../shop-cart/shop-cart.vue"

	export default {
		components: {
			tabBar,
			shop,
			order,
			myAccount,
			shopCart
		},
		props: {
			tarIndex: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				tarIndex: 0,
				state: {},
				statusList: [],
				showModel: false,
				content: `您有新的订单等待收货！<br> 此消息只提醒一次`,
				title: '订单提示',
				showMessage: "",
			}
		},
		onLoad() {
			// 监测用户是否登录，未登录则重新登录
			let token = uni.getStorageSync("token");
			if (!token) {
				uni.showToast({
					title: "请重新登录",
					icon: "error"
				})
				setTimeout(() => {
					uni.reLaunch({
						url: "/pages/login/login"
					})
				}, 1300)
			}
			this.getStatus();
		},
		methods: {
			// 登录页跳转
			goPage() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},

			// 改变选择底部栏的样子
			changeTab(index) {
				this.tarIndex = index;
			},

			getStatus() {
				var getStatusInter = setInterval(() => {
					this.$ajax.getOrderList.get().then(res => {
						if (res.code == 1 && res.data.length != 0) {
							this.statusList = res.data;
							this.statusList.map(item => {
								if (item.status == 3 && this.showMessage.indexOf(item.id) == -1) {
									this.showModel = true;
									this.showMessage += item.id + ','
								}
							})
						}
					})
				}, 5000)
			},

			closeModel() {
				this.showModel = false;
				this.tarIndex = 1
			}
		},
		beforeDestroy() {
			if (this.getStatusInter) {
				clearInterval(this.getStatusInter);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.outer {}
</style>
